import { Tag as TagEntity } from '@/entity/Tag';
import { PaginationConfig } from '@/types';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { DataSource, DeepPartial, In, Like, Repository } from 'typeorm';
import { CommonService } from '../common/common.service';

@Injectable()
export class TagService extends CommonService<TagEntity> {
  constructor(
    @InjectRepository(TagEntity)
    private readonly tagRepository: Repository<TagEntity>,
    private dataSource: DataSource,
  ) {
    super(tagRepository);
  }

  async getTagsByIds(tagIds: number[]) {
    return await this.tagRepository.find({
      where: { id: In(tagIds) },
    });
  }

  async findAllAndCount(
    findOption: DeepPartial<TagEntity>,
    pagination: PaginationConfig,
  ) {
    const [list, total] = await this.tagRepository.findAndCount({
      where: {
        name: findOption.name ? Like(`%${findOption.name}%`) : undefined,
        // id 肯定为正整数
        id: findOption.id ? findOption.id : undefined,
      },
      skip: pagination.pageSize * (pagination.current - 1),
      take: pagination.pageSize,
    });
    return {
      list,
      total,
    };
  }
}
